home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH2 / SRC / MMETA.FRM < prev    next >
Text File  |  1996-04-18  |  6KB  |  222 lines

  1. VERSION 4.00
  2. Begin VB.Form MMetaForm 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Memory Metafile"
  5.    ClientHeight    =   3660
  6.    ClientLeft      =   2100
  7.    ClientTop       =   1845
  8.    ClientWidth     =   5580
  9.    Height          =   4350
  10.    Left            =   2040
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   3660
  13.    ScaleWidth      =   5580
  14.    Top             =   1215
  15.    Width           =   5700
  16.    Begin VB.CommandButton CmdClear 
  17.       Caption         =   "Clear"
  18.       Height          =   495
  19.       Left            =   960
  20.       TabIndex        =   6
  21.       Top             =   2880
  22.       Width           =   735
  23.    End
  24.    Begin VB.CommandButton CmdCopy 
  25.       Caption         =   "Copy"
  26.       Height          =   495
  27.       Left            =   120
  28.       TabIndex        =   5
  29.       Top             =   2880
  30.       Width           =   735
  31.    End
  32.    Begin VB.PictureBox Dest 
  33.       AutoRedraw      =   -1  'True
  34.       Height          =   1815
  35.       Index           =   3
  36.       Left            =   3760
  37.       ScaleHeight     =   117
  38.       ScaleMode       =   3  'Pixel
  39.       ScaleWidth      =   117
  40.       TabIndex        =   4
  41.       Top             =   1840
  42.       Width           =   1815
  43.    End
  44.    Begin VB.PictureBox Dest 
  45.       AutoRedraw      =   -1  'True
  46.       Height          =   1815
  47.       Index           =   2
  48.       Left            =   1920
  49.       ScaleHeight     =   117
  50.       ScaleMode       =   3  'Pixel
  51.       ScaleWidth      =   117
  52.       TabIndex        =   3
  53.       Top             =   1840
  54.       Width           =   1815
  55.    End
  56.    Begin VB.PictureBox Dest 
  57.       AutoRedraw      =   -1  'True
  58.       Height          =   1815
  59.       Index           =   1
  60.       Left            =   3760
  61.       ScaleHeight     =   117
  62.       ScaleMode       =   3  'Pixel
  63.       ScaleWidth      =   117
  64.       TabIndex        =   2
  65.       Top             =   0
  66.       Width           =   1815
  67.    End
  68.    Begin VB.PictureBox Dest 
  69.       AutoRedraw      =   -1  'True
  70.       Height          =   1815
  71.       Index           =   0
  72.       Left            =   1920
  73.       ScaleHeight     =   117
  74.       ScaleMode       =   3  'Pixel
  75.       ScaleWidth      =   117
  76.       TabIndex        =   1
  77.       Top             =   0
  78.       Width           =   1815
  79.    End
  80.    Begin VB.PictureBox Source 
  81.       AutoRedraw      =   -1  'True
  82.       Height          =   1815
  83.       Left            =   0
  84.       ScaleHeight     =   117
  85.       ScaleMode       =   3  'Pixel
  86.       ScaleWidth      =   117
  87.       TabIndex        =   0
  88.       Top             =   960
  89.       Width           =   1815
  90.    End
  91.    Begin VB.Menu mnuFile 
  92.       Caption         =   "&File"
  93.       Begin VB.Menu mnuFileExit 
  94.          Caption         =   "E&xit"
  95.       End
  96.    End
  97. End
  98. Attribute VB_Name = "MMetaForm"
  99. Attribute VB_Creatable = False
  100. Attribute VB_Exposed = False
  101. Option Explicit
  102.  
  103. Dim Drawing As Boolean
  104. Dim PointX() As Single
  105. Dim PointY() As Single
  106. Dim NumPoints As Integer
  107. Dim LastPoint As Integer
  108. Dim LastX As Single
  109. Dim LastY As Single
  110.  
  111.  
  112.  
  113. Private Sub CmdClear_Click()
  114.     Source.Cls
  115.     LastPoint = 0
  116. End Sub
  117.  
  118. Private Sub Source_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  119.     Drawing = True
  120.     LastX = x
  121.     LastY = y
  122.     AddPoint -x, y
  123. End Sub
  124. ' ***********************************************
  125. ' Add a point to the list of points.
  126. ' ***********************************************
  127. Sub AddPoint(x As Single, y As Single)
  128.     LastPoint = LastPoint + 1
  129.     If LastPoint > NumPoints Then
  130.         NumPoints = NumPoints + 100
  131.         ReDim Preserve PointX(1 To NumPoints)
  132.         ReDim Preserve PointY(1 To NumPoints)
  133.     End If
  134.     PointX(LastPoint) = x
  135.     PointY(LastPoint) = y
  136.  
  137.     If x < 0 Then
  138.         Source.CurrentX = -x
  139.         Source.CurrentY = y
  140.     Else
  141.         Source.Line -(x, y)
  142.     End If
  143. End Sub
  144.  
  145. Private Sub Source_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  146.     If Not Drawing Then Exit Sub
  147.     
  148.     AddPoint x, y
  149. End Sub
  150.  
  151. Private Sub Source_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  152.     If Not Drawing Then Exit Sub
  153.     Drawing = False
  154.     
  155.     AddPoint x, y
  156. End Sub
  157.  
  158. Private Sub mnuFileExit_Click()
  159.     Unload Me
  160. End Sub
  161.  
  162. ' ***********************************************
  163. ' Create a memory metafile and play it back into
  164. ' the destination picture boxes.
  165. ' ***********************************************
  166. Private Sub CmdCopy_Click()
  167. Dim i As Integer
  168. Dim mhdc As Integer
  169. Dim hMF As Integer
  170. Dim status As Long
  171. Dim x As Single
  172. Dim y As Single
  173.  
  174.     ' Create the memory metafile.
  175.     mhdc = CreateMetaFile(ByVal 0&)
  176.     If mhdc = 0 Then
  177.         Beep
  178.         MsgBox "Error creating metafile.", vbExclamation
  179.         Exit Sub
  180.     End If
  181.     
  182.     ' Draw in the metafile.
  183.     For i = 1 To LastPoint
  184.         x = PointX(i)
  185.         y = PointY(i)
  186.         If x < 0 Then
  187. #If Win32 Then
  188.             status = MoveToEx(mhdc, -x, y, ByVal 0&)
  189. #Else
  190.             status = MoveTo(mhdc, -x, y)
  191. #End If
  192.         Else
  193.             status = LineTo(mhdc, x, y)
  194.         End If
  195.     Next i
  196.     
  197.     ' Close the metafile.
  198.     hMF = CloseMetaFile(mhdc)
  199.     If hMF = 0 Then
  200.         Beep
  201.         MsgBox "Error closing metafile.", vbExclamation
  202.     Else
  203.         ' Play the metafile.
  204.         For i = 0 To 3
  205.             Dest(i).Cls
  206.             If PlayMetaFile(Dest(i).hdc, hMF) = 0 Then
  207.                 Beep
  208.                 MsgBox "Error playing metafile.", vbExclamation
  209.                 Exit For
  210.             End If
  211.             Dest(i).Refresh
  212.         Next i
  213.     End If
  214.     
  215.     ' Delete the metafile.
  216.     If DeleteMetaFile(hMF) = 0 Then
  217.         Beep
  218.         MsgBox "Error deleting metafile.", vbExclamation
  219.     End If
  220. End Sub
  221.  
  222.